package cn.oa.service.impl;

import cn.oa.dao.AttendanceDao;
import cn.oa.entity.Attendance;
import cn.oa.entity.AttendanceVo;
import cn.oa.service.AttendanceService;
import cn.oa.util.Page;
import cn.oa.util.SessionFatoryUtil;
import org.apache.ibatis.session.SqlSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * @Description TODO
 * @Author hyl
 * @Date 2022/3/29 9:05
 **/
@Service("attendanceService")

public class AttendanceServiceImpl implements AttendanceService {
	//SqlSession sqlSession = SessionFatoryUtil.getSqlSession();
	@Autowired
	private AttendanceDao attendanceDao ;//= sqlSession.getMapper(AttendanceDao.class);

	@Override
	public int addSignInByEmployeeId(int employeeId, String date) throws SQLException {

		int result = 0;
		try{
			//查询员工是否有上班打卡记录
			Attendance record = attendanceDao.getRecordByEmployeeIdAndDate(employeeId, date);
			if(record==null){
				//执行打卡
				result = attendanceDao.addSinginByEmployeeId(employeeId);
			}else{
				//已完成上班打卡,不允许重复打卡
				result = 0;
			}
			//sqlSession.commit();
		}catch(SQLException e){
			e.printStackTrace();
			//sqlSession.rollback();
			throw e;
		}finally {
			//SessionFatoryUtil.closeSqlSession(sqlSession);
		}
		return result;
	}

	@Override
	public int addSignOutByEmployeeId(int employeeId, String date) throws SQLException {
		//AttendanceDao attendanceDao = sqlSession.getMapper(AttendanceDao.class);
		int result = 0;
		try{

			//查询员工是否有上班打卡记录
			Attendance record = attendanceDao.getRecordByEmployeeIdAndDate(employeeId, date);
			if(record==null){
				//没有上班打卡记录时不允许执行下班打卡操作
				result = 0;
			}else{
				if(record.getSignOutTime()==null){
					//执行下班打卡
					result = attendanceDao.updateSingoutByAttendanceId(record.getId());
				}else{
					result = -2;
				}
			}
			//sqlSession.commit();
		}catch(SQLException e){
			e.printStackTrace();
			//sqlSession.rollback();
			throw e;
		}finally {
			//SessionFatoryUtil.closeSqlSession(sqlSession);
		}
		return result;
	}

	@Override
	public void getAttendancesByPage(String name, String searchDate, Page pageObj) throws SQLException {
		//AttendanceDao attendanceDao = sqlSession.getMapper(AttendanceDao.class);
		AttendanceVo vo = new AttendanceVo();
		vo.setName(name);
		vo.setSearchDate(searchDate);
		vo.setBeginIndex(pageObj.getBeginIndex());
		vo.setPageSize(pageObj.getPageSize());
		try {
			int totalCount = attendanceDao.getAttendancesCount(vo);
			pageObj.setTotalCount(totalCount);
			if (totalCount > 0) {
				if (pageObj.getCurrPageNo() > pageObj.getTotalPageCount()) {
					pageObj.setCurrPageNo(pageObj.getTotalPageCount());
				}
				List<Attendance> attendanceList = attendanceDao.getAttendancesByPage(vo);
				pageObj.setAttendanceList(attendanceList);
			} else {
				pageObj.setCurrPageNo(0);
				pageObj.setAttendanceList(new ArrayList<Attendance>());
			}

		}catch(SQLException e){
			e.printStackTrace();
			throw e;
		}finally{
			//SessionFatoryUtil.closeSqlSession(sqlSession);
		}
	}

	public void setAttendanceDao(AttendanceDao attendanceDao) {
		this.attendanceDao = attendanceDao;
	}
}
